//
//  BarberDetailViewController.m
//  HeadYang
//
//  Created by sunmingming on 15/7/18.
//  Copyright (c) 2015年 xiaoluoli. All rights reserved.
//

#import "BarberDetailViewController.h"
#import "BarberTimeView.h"
#import "BarberAlertView.h"
#import "DKScrollingTabController.h"
#import "JianFaViewController.h"
#import "DDViewController.h"
#import "UIImageView+AFNetworking.h"
#import "RFChoice1ViewController.h"

@interface BarberDetailViewController ()<UITableViewDataSource,UITableViewDelegate,BarberTimeDelegate,BarberAlertDelegate,DKScrollingTabControllerDelegate>

@property(nonatomic,weak) UIButton *teleButton;
@property(nonatomic,weak) UIButton *phonButton;

@property(nonatomic,weak) IBOutlet UITableView *listView;
@property(nonatomic,strong) DKScrollingTabController *leftTabController;
@property(nonatomic,assign) NSInteger tabIndex;
@property(nonatomic,strong) NSMutableArray *barberWorksArray;
@property(nonatomic,assign) BOOL hasAppeared;

@end

@implementation BarberDetailViewController

@synthesize tabIndex;
@synthesize listView;
@synthesize leftTabController;
@synthesize barberWorksArray = _barberWorksArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
        self.hasAppeared = NO;
        self.barberWorksArray = [NSMutableArray array];
        APPALL.orderDetailDic = nil;
        // Add controller as a child view controller (standard view controller containment)
        self.tabIndex = 0;
        self.leftTabController = [[DKScrollingTabController alloc] init];
        [self.view addSubview:leftTabController.view];
        leftTabController.view.frame = CGRectMake(0,0,kDeviceWidth,50);
        leftTabController.view.backgroundColor = [UIColor whiteColor];
        leftTabController.buttonPadding = 10;
        leftTabController.underlineIndicator = YES;
        leftTabController.underlineIndicatorColor = [UIColor redColor];
        leftTabController.buttonsScrollView.showsHorizontalScrollIndicator = NO;
        leftTabController.selectedBackgroundColor = [UIColor clearColor];
        leftTabController.selectedTextColor = [UIColor blackColor];
        leftTabController.unselectedTextColor = [UIColor grayColor];
        leftTabController.unselectedBackgroundColor = [UIColor clearColor];
        leftTabController.startingIndex = tabIndex;
        leftTabController.delegate = self;
        leftTabController.buttonTitleEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 0);
        
        leftTabController.selection = @[@"1", @"2", @"3"];
        [leftTabController setButtonName:@"个人主页" atIndex:0];
        [leftTabController setButtonName:@"个人介绍" atIndex:1];
        [leftTabController setButtonName:@"个人作品" atIndex:2];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(self.hasAppeared){
        return;
    }
    self.hasAppeared = YES;
    if(self.barberDic){
        self.title = [tooles replaceNullFromDic:self.barberDic withKeyArray:@[@"name"]];
        NSDictionary *para =
        @{@"id":[self.barberDic valueForKey:@"id"],
          @"token":APPALL.myUserItem.token};
        [SVProgressHUD showWithStatus:@"loading..."];
        [AVCloud callFunctionInBackground:@"seeBarber" withParameters:para block:^(id object, NSError *error) {
            // 执行结果
            if (!error && object) {
                NSLog(@"avget:%@",object);
                NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
                switch ([[aSucData valueForKey:@"success"] intValue]) {
                    case 0:
                    {
                        [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    }
                        break;
                    case 1:
                    {
                        APPALL.barberDetailDic = [aSucData valueForKey:@"barber"];
                        [self getBarberStore];
                    }
                        break;
                    default:
                        break;
                }
                [SVProgressHUD dismiss];
            } else {
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
            }
        }];
    }
}

-(void)getBarberStore{
    NSDictionary *para =
    @{@"id":[APPALL.barberDetailDic valueForKey:@"store"],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"store" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    APPALL.barberStoreDic = [aSucData objectForKey:@"store"];
                    [self getBarberWorks];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)getBarberWorks{
    NSDictionary *para =
    @{@"id":[APPALL.barberDetailDic valueForKey:@"id"],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"barber_seeworks" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    [_barberWorksArray removeAllObjects];
                    NSArray *cityArray = [aSucData objectForKey:@"barber"];
                    for (NSDictionary *dic in cityArray) {
                        [_barberWorksArray addObject:dic];
                    }
                    [self getBarberPrice];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)getBarberPrice{
    NSDictionary *para =
    @{@"id":[APPALL.barberDetailDic valueForKey:@"id"],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"barber_service" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    NSArray *cityArray = [aSucData objectForKey:@"servicePrice"];
                    for (NSDictionary *dic in cityArray) {
                        switch (APPALL.myType) {
                            case hairJianFa:{
                                if([[dic valueForKey:@"service"] isEqualToString:@"剪发"]){
                                    APPALL.barberServiceDic = dic;
                                }
                            }
                                break;
                            case hairTangFa:{
                                if([[dic valueForKey:@"service"] isEqualToString:@"烫发"]){
                                    APPALL.barberServiceDic = dic;
                                }
                            }
                                break;
                            case hairRanFa:{
                                if([[dic valueForKey:@"service"] isEqualToString:@"染发"]){
                                    APPALL.barberServiceDic = dic;
                                }
                            }
                                break;
                            case hairHuli:{
                                if([[dic valueForKey:@"service"] isEqualToString:@"护理"]){
                                    APPALL.barberServiceDic = dic;
                                }
                            }
                                break;
                            default:
                                break;
                        }
                    }
                    [listView reloadData];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(!APPALL.barberDetailDic)
        return 0;
    switch (tabIndex) {
        case 0:
            return 2;
        case 1:
            return 1;
        case 2:
            return 1;
        default:
            return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (tabIndex) {
        case 0:
            switch (section) {
                case 0:
                    return 4;
                case 1:
                    return 1;
                default:
                    return 0;
            }
        case 1:{
            int rowNum = 3;
            if([[APPALL.barberDetailDic valueForKey:@"popularMark"] boolValue]){
                rowNum++;
            }
            if([[APPALL.barberDetailDic valueForKey:@"starMark"] boolValue]){
                rowNum++;
            }
            return rowNum;
        }
        case 2:
            if(self.barberWorksArray.count <= 0)
                return 0;
            else
                return (([self.barberWorksArray count] - 1)/3 + 1);
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    switch (tabIndex) {
        case 0:
            switch (section) {
                case 0:
                    return 10.0f;
                default:
                    return 0.0f;
            }
        case 1:
            return 0;
        case 2:
            return 0;
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (tabIndex) {
        case 0:
            switch (indexPath.section) {
                case 0:
                    switch (indexPath.row) {
                        case 0:
                            return 100.0f;
                        case 1:
                            return 40.0f;
                        case 2:
                            return 110.0f;
                        case 3:
                            return 40.0f;
                        default:
                            return 0;
                    }
                case 1:
                    return 44.0f;
                case 2:
                    switch (indexPath.row) {
                        case 0:
                            return 60.0f;
                        default:
                            return 80.0f;
                    }
                default:
                    return 0;
            }
        case 1:{
            int rowNum = 0;
            if([[APPALL.barberDetailDic valueForKey:@"popularMark"] boolValue]){
                rowNum++;
            }
            if([[APPALL.barberDetailDic valueForKey:@"starMark"] boolValue]){
                rowNum++;
            }
            NSInteger rowReal = indexPath.row - rowNum;
            if(rowReal < 0) return 20.0f;
            else if(rowReal == 0) return 120.0f;
            else if(rowReal == 1) return 92.0f;
            else if(rowReal == 2) return 112.0f;
            else return 0.0f;
        }
        default:
            return 0;
    }
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *inde;
    switch (tabIndex) {
        case 0:
            switch (indexPath.section) {
                case 0:
                    switch (indexPath.row) {
                        case 0:{
                            inde = @"BarberHeadID";
                            cell = [tableView dequeueReusableCellWithIdentifier:inde];
                            if (cell == nil) {
                                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberHeadCell" owner:self options:nil];
                                cell = [nib objectAtIndex:0];
                            }
                            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
                            UILabel *lab4 = (UILabel*)[cell viewWithTag:4];
                            UIImageView *img1 = (UIImageView*)[cell viewWithTag:11];
                            UIImageView *img2 = (UIImageView*)[cell viewWithTag:12];
                            UIImageView *img3 = (UIImageView*)[cell viewWithTag:13];
                            UIImageView *img4 = (UIImageView*)[cell viewWithTag:14];
                            UIImageView *img5 = (UIImageView*)[cell viewWithTag:15];
                            UIImageView *headImg = (UIImageView*)[cell viewWithTag:99];
                            lab1.text = [tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"name"]];
                            lab2.text = [NSString stringWithFormat:@"接单%@次",[tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"orderExperience"]]];
                            lab4.text = [NSString stringWithFormat:@"￥%@",[APPALL.barberDetailDic valueForKey:@"averagePrice"]];
                            NSString *headImage = [APPALL.barberDetailDic valueForKey:@"headImage"];
                            if(![headImage isEqual:[NSNull null]]
                               && headImage
                               && headImage.length){
                                [headImg setImageWithURL:[NSURL URLWithString:headImage]
                                        placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
                            }
                            NSInteger score = [[APPALL.barberDetailDic valueForKey:@"score"] integerValue];
                            img1.hidden = YES;
                            img2.hidden = YES;
                            img3.hidden = YES;
                            img4.hidden = YES;
                            img5.hidden = YES;
                            if(score > 0){img1.hidden = NO;}
                            if(score > 20){img2.hidden = NO;}
                            if(score > 40){img3.hidden = NO;}
                            if(score > 60){img4.hidden = NO;}
                            if(score > 80){img5.hidden = NO;}
                        }
                            break;
                        case 1:{
                            inde = @"BarberScoreID";
                            cell = [tableView dequeueReusableCellWithIdentifier:inde];
                            if (cell == nil) {
                                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberScore" owner:self options:nil];
                                cell = [nib objectAtIndex:0];
                            }
                            if(!APPALL.barberDetailDic)
                                break;
                            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
                            UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
                            lab1.text = [NSString stringWithFormat:@"专业:%@",[tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"profession"]]];
                            lab2.text = [NSString stringWithFormat:@"沟通:%@",[tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"communicate"]]];
                            lab3.text = [NSString stringWithFormat:@"守时:%@",[tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"punctual"]]];
                        }
                            break;
                        case 2:{
                            inde = @"BarberLocID";
                            cell = [tableView dequeueReusableCellWithIdentifier:inde];
                            if (cell == nil) {
                                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberLoc" owner:self options:nil];
                                cell = [nib objectAtIndex:0];
                            }
                            if(!APPALL.barberStoreDic)
                                break;
                            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
                            lab1.text = [tooles replaceNullFromDic:APPALL.barberStoreDic withKeyArray:@[@"name"]];
                            lab2.text = [tooles replaceNullFromDic:APPALL.barberStoreDic withKeyArray:@[@"address"]];
                        }
                            break;
                        case 3:{
                            inde = @"StorePhoneID";
                            cell = [tableView dequeueReusableCellWithIdentifier:inde];
                            if (cell == nil) {
                                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorePhone" owner:self options:nil];
                                cell = [nib objectAtIndex:0];
                            }
                            if(!APPALL.barberStoreDic)
                                break;
                            if(!self.teleButton){
                                self.teleButton = (UIButton*)[cell viewWithTag:1];
                                [self.teleButton addTarget:self action:@selector(telePressed:) forControlEvents:UIControlEventTouchUpInside];
                            }
                            if(!self.phonButton){
                                self.phonButton = (UIButton*)[cell viewWithTag:2];
                                [self.phonButton addTarget:self action:@selector(phonPressed:) forControlEvents:UIControlEventTouchUpInside];
                            }
                        }
                            break;
                        default:
                            break;
                    }
                    break;
                case 1:{
                    inde = @"barbermidid";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberMid" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:{
            int rowNum = 0;
            if([[APPALL.barberDetailDic valueForKey:@"popularMark"] boolValue]){
                rowNum++;
            }
            if([[APPALL.barberDetailDic valueForKey:@"starMark"] boolValue]){
                rowNum++;
            }
            NSInteger rowReal = indexPath.row - rowNum;
            if(rowReal == -2){
                inde = @"barberrewardid";
                cell = [tableView dequeueReusableCellWithIdentifier:inde];
                if (cell == nil) {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberReward" owner:self options:nil];
                    cell = [nib objectAtIndex:0];
                }
                UILabel *lab = (UILabel*)[cell viewWithTag:1];
                UIImageView *img = (UIImageView*)[cell viewWithTag:2];
                lab.text = @"最受欢迎美发师认证";
                img.image = [UIImage imageNamed:@"a.png"];
            }else if(rowReal == -1){
                inde = @"barberrewardid";
                cell = [tableView dequeueReusableCellWithIdentifier:inde];
                if (cell == nil) {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberReward" owner:self options:nil];
                    cell = [nib objectAtIndex:0];
                }
                UILabel *lab = (UILabel*)[cell viewWithTag:1];
                UIImageView *img = (UIImageView*)[cell viewWithTag:2];
                if([[tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"starMark"]] boolValue]){
                    lab.text = @"超级明星理发师认证";
                    img.image = [UIImage imageNamed:@"b.png"];
                }else{
                    lab.text = @"最受欢迎美发师认证";
                    img.image = [UIImage imageNamed:@"a.png"];
                }
            }else if(rowReal == 0){
                inde = @"BarberStoryID";
                cell = [tableView dequeueReusableCellWithIdentifier:inde];
                if (cell == nil) {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberStory" owner:self options:nil];
                    cell = [nib objectAtIndex:0];
                }
                UITextView *tv1 = (UITextView*)[cell viewWithTag:1];
                tv1.text = [tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"intro"]];
            }else if(rowReal == 1){
                inde = @"BarberHobbitID";
                cell = [tableView dequeueReusableCellWithIdentifier:inde];
                if (cell == nil) {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberHobbit" owner:self options:nil];
                    cell = [nib objectAtIndex:0];
                }
                if(!APPALL.barberDetailDic)
                    break;
                UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
                UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
                lab1.text = [tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"origin"]];
                lab2.text = [tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"beFondOf"]];
                lab3.text = [tooles replaceNullFromDic:APPALL.barberDetailDic withKeyArray:@[@"constellation"]];
            }else if(rowReal == 2){
                inde = @"BarBerPicshowID";
                cell = [tableView dequeueReusableCellWithIdentifier:inde];
                if (cell == nil) {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberPicShow" owner:self options:nil];
                    cell = [nib objectAtIndex:0];
                }
                UIImageView *img1 = (UIImageView*)[cell viewWithTag:1];
                UIImageView *img2 = (UIImageView*)[cell viewWithTag:2];
                UIImageView *img3 = (UIImageView*)[cell viewWithTag:3];
                if(self.barberWorksArray.count <= 0){
                    img1.hidden = YES;
                }else{
                    img1.hidden = NO;
                    [img1 setImageWithURL:[NSURL URLWithString:@"http://luckystudio.cn:8080/HCB/static/images/barber/img1.jpg"]
                               placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
                }
                if(self.barberWorksArray.count <= 1){
                    img2.hidden = YES;
                }else{
                    img2.hidden = NO;
                    [img2 setImageWithURL:[NSURL URLWithString:@"http://luckystudio.cn:8080/HCB/static/images/barber/img1.jpg"]
                            placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
                }
                if(self.barberWorksArray.count <= 2){
                    img3.hidden = YES;
                }else{
                    img3.hidden = NO;
                    [img3 setImageWithURL:[NSURL URLWithString:@"http://luckystudio.cn:8080/HCB/static/images/barber/img1.jpg"]
                            placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
                }
            }
        }
            break;
        case 2:{
            inde = @"BarBerPicshowID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberPicShow" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UIImageView *img1 = (UIImageView*)[cell viewWithTag:1];
            UIImageView *img2 = (UIImageView*)[cell viewWithTag:2];
            UIImageView *img3 = (UIImageView*)[cell viewWithTag:3];
            if(self.barberWorksArray.count <= 0 + indexPath.row * 3){
                img1.hidden = YES;
            }else{
                img1.hidden = NO;
                [img1 setImageWithURL:[NSURL URLWithString:@"http://luckystudio.cn:8080/HCB/static/images/barber/img1.jpg"]
                        placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
            }
            if(self.barberWorksArray.count <= 1 + indexPath.row * 3){
                img2.hidden = YES;
            }else{
                img2.hidden = NO;
                [img2 setImageWithURL:[NSURL URLWithString:@"http://luckystudio.cn:8080/HCB/static/images/barber/img1.jpg"]
                        placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
            }
            if(self.barberWorksArray.count <= 2 + indexPath.row * 3){
                img3.hidden = YES;
            }else{
                img3.hidden = NO;
                [img3 setImageWithURL:[NSURL URLWithString:@"http://luckystudio.cn:8080/HCB/static/images/barber/img1.jpg"]
                        placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)telePressed:(id)sender{
    NSString *phoneStr = [tooles replaceNullFromDic:APPALL.barberStoreDic withKeyArray:@[@"bizPhone"]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneStr]]];
}

-(void)phonPressed:(id)sender{
    NSString *phoneStr = [tooles replaceNullFromDic:APPALL.barberStoreDic withKeyArray:@[@"phone"]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneStr]]];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section != 1 || tabIndex != 0)
        return;
    if([APPALL.myUserItem.username isEqualToString:@"11111111111"]){
        [tooles showGlobalAlertWithTitle:@"请登录" andMsg:@"您当前身份是游客，请返回登陆界面以顾客身份注册登录才能使用预约功能。" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
        return;
    }
    BarberTimeView *timeView = [[BarberTimeView alloc] initWithDetailDic:APPALL.barberDetailDic andServiceDic:APPALL.barberServiceDic];
    timeView.myDelegate = self;
    [[KGModal sharedInstance] showWithContentView:timeView andAnimated:YES];
}

- (void)ScrollingTabController:(DKScrollingTabController *)controller selection:(NSUInteger)selection {
    self.tabIndex = selection;
    NSLog(@"Selection controller action button with index=%@", @(selection));
    [self.listView reloadData];
}

-(void)yyPressed:(id)sender {
    [[KGModal sharedInstance] hideAnimated:NO];
    BarberAlertView *cacheView = [BarberAlertView new];
    cacheView.myDelegate = self;
    [[KGModal sharedInstance] showWithContentView:cacheView andAnimated:NO];
}

-(void)alertOKPressed:(id)sender{
    NSLog(@"OKKKK");
    [[KGModal sharedInstance] hideAnimated:YES];
    switch (APPALL.myType) {
        case hairJianFa:{
            [self createOrder];
        }
            break;
        case hairHuli:
        case hairRanFa:
        case hairTangFa:{
            RFChoice1ViewController *vc = [RFChoice1ViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

-(void)createOrder{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [dateFormatter stringFromDate:APPALL.selectDate];
    NSString *couponStr = APPALL.couponDic?[APPALL.couponDic valueForKey:@"couponNum"]:@"";
    NSDictionary *para =
    @{@"date":dateStr,
      @"barber":[APPALL.barberDetailDic valueForKey:@"id"],
      @"startTimePiece":[NSString stringWithFormat:@"%i",APPALL.startTimeFlag],
      //      @"gender":@"",
      //      @"hairType":@"",
      @"coupon":couponStr,
      @"memo":@"",
      @"expected":@"100",
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"order_create" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    APPALL.orderDetailDic = [aSucData valueForKey:@"order"];
                    [self addService];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)addService{
    NSDictionary *para =
    @{@"id":[APPALL.orderDetailDic valueForKey:@"id"],
      @"type":[NSString stringWithFormat:@"%i",APPALL.myType],
      @"postAddress":@"",
      @"goods1":@"",
      @"goods2":@"",
      @"goods3":@"",
      @"goods4":@"",
      @"goods5":@"",
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"order_service" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 1:
                {
                    DDViewController *vc = [DDViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)alertCancelPressed:(id)sender{
    [[KGModal sharedInstance] hideAnimated:YES];
}

@end